<template>
  <div>
    <header class="home-header wrap" :class="{'active' : headerScroll}">
      <div class="header-search">
        <span class="app-name">毕设商城</span>
        <i class="iconfont icon-search"></i>
        <router-link tag="span" class="search-title" to="./search?from=home">望疫情早日结束</router-link>
      </div>
      <router-link class="login" tag="span" to="./login" v-if="!isLogin">登录</router-link>
      <router-link class="login" tag="span" to="./user" v-else>
        <van-icon name="manager-o"/>
      </router-link>
    </header>

    <nav-bar/>

    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="#1baeae">
      <van-swipe-item v-for="(item, index) in swiperList" :key="index">
        <img :src="item.pictures" alt="" @click="goToDetail(item)">
      </van-swipe-item>
    </van-swipe>

    <div class="category-list">
      <div v-for="item in categoryTypeList" :key="item.categoryTypeId" @click="goToSearch(item)">
        <img :src="item.imgUrl" alt="">
        <span>{{ item.name }}</span>
      </div>
    </div>

    <div class="good">
      <header class="good-header">热门商品</header>
      <van-skeleton title :row="3" :loading="loading">
        <div class="good-box">
          <div class="good-item" v-for="item in hots" :key="item.goodId" @click="goToDetail(item)">
            <img :src="item.pictures" alt="">
            <div class="good-desc">
              <div class="title">{{ item.name }}</div>
              <div :style="{color:'red'}" v-if="item.isSeckill===1">秒杀活动中</div>
              <div class="price">¥ {{ item.sellingPrice }}</div>
            </div>
          </div>
        </div>
      </van-skeleton>
    </div>

  </div>
</template>

<script>
import {reactive, onMounted, toRefs, nextTick} from 'vue'
import {useRouter} from 'vue-router'
import navBar from '@/components/NavBar'
import {getHome} from '@/service/home'
import {getLocal} from '@/common/js/local-storge'
import {Toast} from 'vant'

export default {
  name: 'v-home',
  components: {
    navBar
  },
  setup() {
    const router = useRouter()
    const state = reactive({
      swiperList: [], // 轮播图列表
      isLogin: false, // 是否已登录
      headerScroll: false, // 滚动透明判断
      categoryTypeList: [
        {
          name: '内存',
          imgUrl: 'https://img0.baidu.com/it/u=3418663229,2663876057&fm=253&fmt=auto&app=138&f=JPEG?w=640&h=425',
          categoryTypeId: 100001
        }, {
          name: '电源',
          imgUrl: 'https://img2.baidu.com/it/u=830354620,660084575&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
          categoryTypeId: 100003
        }, {
          name: '显示器',
          imgUrl: 'https://img1.baidu.com/it/u=1506382938,3200962786&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
          categoryTypeId: 100002
        }, {
          name: '鼠标',
          imgUrl: 'http://t15.baidu.com/it/u=2707982515,4125184227&fm=224&app=112&f=JPEG?w=500&h=500',
          categoryTypeId: 100004
        }, {
          name: '键盘',
          imgUrl: 'https://img1.baidu.com/it/u=1179319299,1673939068&fm=253&fmt=auto&app=120&f=JPEG?w=711&h=294',
          categoryTypeId: 100005
        }
      ],
      hots: [],
      loading: true
    })
    onMounted(async () => {
      const token = getLocal('token')
      if (token) {
        state.isLogin = true
      }
      Toast.loading({
        message: '加载中...',
        forbidClick: true
      });
      const {swiperList, hotList} = await getHome()
      state.swiperList = swiperList
      state.hots = hotList
      state.loading = false
      Toast.clear()
    })

    nextTick(() => {
      window.addEventListener('scroll', () => {
        let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
        scrollTop > 100 ? state.headerScroll = true : state.headerScroll = false
      })
    })

    const goToDetail = (item) => {
      router.push({path: '/product', query: {goodId: item.goodId}})
    }

    const goToSearch = (item) => {
      router.push({path: '/search', query: {keyword: item.name, searchType: 'type'}})
    }

    const tips = () => {
      Toast('敬请期待');
    }


    return {
      ...toRefs(state),
      goToDetail,
      goToSearch,
      tips
    }
  },
}
</script>

<style lang="less" scoped>
@import '../../common/style/mixin';

.home-header {
  position: fixed;
  left: 0;
  top: 0;
  .wh(100%, 50px);
  .fj();
  line-height: 50px;
  padding: 0 15px;
  .boxSizing();
  font-size: 15px;
  color: #fff;
  z-index: 10000;

  .nbmenu2 {
    color: @primary;
  }

  &.active {
    background: @primary;

    .nbmenu2 {
      color: #fff;
    }

    .login {
      color: #fff;
    }
  }

  .header-search {
    display: flex;
    .wh(74%, 20px);
    line-height: 20px;
    margin: 10px 0;
    padding: 5px 0;
    color: #232326;
    background: rgba(255, 255, 255, .7);
    border-radius: 20px;

    .app-name {
      padding: 0 10px;
      color: @primary;
      font-size: 20px;
      font-weight: bold;
      border-right: 1px solid #666;
    }

    .icon-search {
      padding: 0 10px;
      font-size: 17px;
    }

    .search-title {
      font-size: 12px;
      color: #666;
      line-height: 21px;
    }
  }

  .icon-iconyonghu {
    color: #fff;
    font-size: 22px;
  }

  .login {
    color: @primary;
    line-height: 52px;

    .van-icon-manager-o {
      font-size: 20px;
      vertical-align: -3px;
    }
  }
}

.category-list {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  width: 100%;
  padding-bottom: 13px;

  div {
    display: flex;
    flex-direction: column;
    width: 20%;
    text-align: center;

    img {
      .wh(36px, 36px);
      margin: 13px auto 8px auto;
    }
  }
}

.good {
  .good-header {
    background: #f9f9f9;
    height: 50px;
    line-height: 50px;
    text-align: center;
    color: @primary;
    font-size: 16px;
    font-weight: 500;
  }

  .good-box {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;

    .good-item {
      box-sizing: border-box;
      width: 50%;
      border-bottom: 1PX solid #e9e9e9;
      padding: 10px 10px;

      img {
        display: block;
        width: 120px;
        margin: 0 auto;
      }

      .good-desc {
        text-align: center;
        font-size: 14px;
        padding: 10px 0;

        .title {
          color: #222333;
        }

        .price {
          color: @primary;
        }
      }

      &:nth-child(2n + 1) {
        border-right: 1PX solid #e9e9e9;
      }
    }
  }
}

.floor-list {
  width: 100%;
  padding-bottom: 50px;

  .floor-head {
    width: 100%;
    height: 40px;
    background: #F6F6F6;
  }

  .floor-content {
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;
    width: 100%;
    .boxSizing();

    .floor-category {
      width: 50%;
      padding: 10px;
      border-right: 1px solid #dcdcdc;
      border-bottom: 1px solid #dcdcdc;
      .boxSizing();

      &:nth-child(2n) {
        border-right: none;
      }

      p {
        font-size: 17px;
        color: #333;

        &:nth-child(2) {
          padding: 5px 0;
          font-size: 13px;
          color: @primary;
        }
      }

      .floor-products {
        .fj();
        width: 100%;

        img {
          .wh(65px, 65px);
        }
      }
    }
  }
}
</style>